package org.baseflexcharts.charts
{
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	
	import org.baseflexcharts.backgrounds.GridBackground;
	import org.baseflexcharts.databoard.YOnlyPointLineDataBoard;
	import org.baseflexcharts.rules.HorizontalRule;
	import org.baseflexcharts.rules.VerticalRule;
	
	public class YOnlyCoordinateChart extends Canvas
	{
		//public values
		//new ArrayCollection([{name: '12-2号', data: [{value: 35, tip: 'xxx'},{}]},{}])		
		private var _dataProvider:ArrayCollection;
		
		private var _yUnitValue:uint = 10;
		
		private var _xOffset:uint = 10;
		
		private var _yOffset:uint = 10;
		
		private var _xLabelOffset:int = 0;
		
		private var _yLabelOffset:int = -15;
		
		private var _coordinateLineColor:uint = 0x000000;
		
		private var _dataColors:Array = new Array();
		
		private var _dataNames:Array = new Array();
		
		private var _backgroundLineColor:uint = 0xdddddd;
		
		private var _lineAlpha:Number = 1;
		
		private var _lineWeight:uint = 2;
		
		private var _xLabelShowInterval:uint = 1;
		
		private var _yLabelShowInterval:uint = 1;
		
		private var _verticalRuleWidth:uint = 70;
		
		private var _horizontalRuleHeight:uint = 70;
		
		//private styles
		private var _rightMargin:uint = 35;

		private var _topMargin:uint = 20;
		
		//privates
		public var xDataProvider:ArrayCollection = new ArrayCollection();
		
		public var yDataProvider:ArrayCollection = new ArrayCollection();
		
		public var dataBoardDataProvider:ArrayCollection = new ArrayCollection();
		
		public var legendHeight:uint = 20;
		
		public function YOnlyCoordinateChart()
		{
			super();
		}
		
		public function draw():void
        {			
			if (dataProvider != null) {					
				
				var maxValue:uint = 0;
				var maxDataLength:uint = 0;
				
				for (var m:uint = 0; m < dataProvider.length; m++)
				{
					var tempObject:Object = dataProvider.getItemAt(i);
					var tempLength:uint = 0;
					
					for (var n:uint = 0; tempObject.data[n] != null; n++) 
					{
						tempLength++;
					}
					
					if (tempLength > maxDataLength)
					{
						maxDataLength = tempLength;
					}
				}
				
				for (var i:uint = 0; i < dataProvider.length; i++)
				{
					var object:Object = dataProvider.getItemAt(i);
					
					this.xDataProvider.addItem({label: object.name});
					
					var dataLength:uint = 0;
					
					for (var l:uint = 0; object.data[l] != null; l++) 
					{
						dataLength++;
					}						
					
					for (var j:uint = 0; j < maxDataLength; j++) 
					{		
						var dataValue:Number = 0;
						var dataTip:String = "";
						
						if (j < dataLength) 
						{
							dataValue = object.data[j].value;
							dataTip = object.data[j].tip;
						}
										
						if (uint(dataValue) > maxValue) {
							maxValue = uint(dataValue);
						}
						
						if (dataBoardDataProvider.length <= j)
						{
							dataBoardDataProvider.addItem(new ArrayCollection());
						}
						
						
						
						this.dataBoardDataProvider[j].addItem({value: dataValue, tip: dataTip});
				    }
				}
				
				for (var k:uint = 0; k <= uint(maxValue / yUnitValue) + 1; k++)
				{
					this.yDataProvider.addItem({label: k * yUnitValue});
				}
			}	
			
			drawMaterials();
			drawVerticalRule();
			drawHorizontalRule();	
			drawBackground();	
			drawDataBoard();	
		}	
		
		protected function drawMaterials():void
		{
			//
		}	
		
		private function drawVerticalRule():void
		{
			var rule:VerticalRule = new VerticalRule();
			
			rule.x = 0;
			rule.y = legendHeight;
			rule.width = this.verticalRuleWidth;
			rule.height = this.height - this.horizontalRuleHeight - legendHeight;
			rule.labelShowInterval = this.yLabelShowInterval;
			rule.labelOffset = this.xLabelOffset;
			rule.lineWeight = this.lineWeight;
			rule.lineColor = this.coordinateLineColor;
			rule.lineAlpha = this.lineAlpha;
			rule.topMargin = this.topMargin;
			rule.bottomMargin = this.yOffset;
			
			rule.labelDataProvider = this.yDataProvider;
			
			rule.draw();
			
			this.addChild(rule);
		}
	    
	    private function drawHorizontalRule():void
	    {
	    	var rule:HorizontalRule = new HorizontalRule();
			
			rule.x = this.verticalRuleWidth;
			rule.y = this.height - this.horizontalRuleHeight;
			rule.width = this.width - this.verticalRuleWidth;
			rule.height = this.horizontalRuleHeight;
			rule.labelShowInterval = this.xLabelShowInterval;
			rule.labelOffset = this.yLabelOffset;
			rule.lineWeight = this.lineWeight;
			rule.lineColor = this.coordinateLineColor;
			rule.lineAlpha = this.lineAlpha;
			rule.leftMargin = this.xOffset;
			rule.rightMargin = this.rightMargin;
			
			rule.labelDataProvider = this.xDataProvider;
			
			rule.draw();
			
			this.addChild(rule);
	    }	    
	    
	    private function drawBackground():void
	    {
	    	var background:GridBackground = new GridBackground();
	    	
	    	background.x = this.verticalRuleWidth;
	    	background.y = legendHeight;
	    	background.width = this.width - this.verticalRuleWidth;
	    	background.height = this.height - this.horizontalRuleHeight - legendHeight;
	    	background.verticalLineAmount = this.xDataProvider.length;
	    	background.horizontalLineAmount = this.yDataProvider.length;
	    	background.leftMargin = this.xOffset;
	    	background.rightMargin = this.rightMargin;
	    	background.topMargin = this.topMargin;
	    	background.bottomMargin = this.yOffset;
	    	background.lineWeight = 1;
	    	background.lineColor = this.backgroundLineColor;
	    	background.lineAlpha = 0.3;
	    	
	    	background.draw();
	    	
	    	this.addChild(background);
	    }
		
		protected function drawDataBoard():void
		{
			//override
		}
		
		public function get dataProvider():ArrayCollection
		{
			return this._dataProvider;
		}
		
		public function set dataProvider(data:ArrayCollection):void
		{
			this._dataProvider = data;
		}
		
		public function get yUnitValue():uint
		{
			return this._yUnitValue;
		}
		
		public function set yUnitValue(data:uint):void
		{
			this._yUnitValue = data;
		}
		
		public function get xOffset():uint
		{
			return this._xOffset;
		}
		
		public function set xOffset(data:uint):void
		{
			this._xOffset = data;
		}
		
		public function get yOffset():uint
		{
			return this._yOffset;
		}
		
		public function set yOffset(data:uint):void
		{
			this._yOffset = data;
		}
		
		public function get xLabelOffset():int
		{
			return this._xLabelOffset;
		}
		
		public function set xLabelOffset(data:int):void
		{
			this._xLabelOffset = data;
		}
		
		public function get yLabelOffset():int
		{
			return this._yLabelOffset;
		}
		
		public function set yLabelOffset(data:int):void
		{
			this._yLabelOffset = data;
		}
		
		public function get lineWeight():uint
		{
			return this._lineWeight;
		}
		
		public function set lineWeight(data:uint):void
		{
			this._lineWeight = data;
		}
		
		public function get coordinateLineColor():uint
		{
			return this._coordinateLineColor;
		}
		
		public function set coordinateLineColor(data:uint):void
		{
			this._coordinateLineColor = data;
		}
		
		public function get backgroundLineColor():uint
		{
			return this._backgroundLineColor;
		}
		
		public function set backgroundLineColor(data:uint):void
		{
			this._backgroundLineColor = data;
		}
		
		public function get dataColors():Array
		{
			return this._dataColors;
		}
		
		public function set dataColors(data:Array):void
		{
			this._dataColors = data;
		}
		
		public function get dataNames():Array
		{
			return this._dataNames;
		}
		
		public function set dataNames(data:Array):void
		{
			this._dataNames = data;
		}
		
		public function get xLabelShowInterval():uint
		{
			return this._xLabelShowInterval;
		}
		
		public function set xLabelShowInterval(data:uint):void
		{
			this._xLabelShowInterval = data;
		}
		
		public function get yLabelShowInterval():uint
		{
			return this._yLabelShowInterval;
		}
		
		public function set yLabelShowInterval(data:uint):void
		{
			this._yLabelShowInterval = data;
		}
		
		public function get verticalRuleWidth():uint
		{
			return this._verticalRuleWidth;
		}
		
		public function set verticalRuleWidth(data:uint):void
		{
			this._verticalRuleWidth = data;
		}
		
		public function get horizontalRuleHeight():uint
		{
			return this._horizontalRuleHeight;
		}
		
		public function set horizontalRuleHeight(data:uint):void
		{
			this._horizontalRuleHeight = data;
		}
		
		public function get lineAlpha():Number
		{
			return this._lineAlpha;
		}
		
		public function set lineAlpha(data:Number):void
		{
			this._lineAlpha = data;
		}	
		
		public function get rightMargin():uint
		{
			return this._rightMargin;
		}
		
		public function set rightMargin(data:uint):void
		{
			this._rightMargin = data;
		}
		
		public function get topMargin():uint
		{
			return this._topMargin;
		}
		
		public function set topMargin(data:uint):void
		{
			this._topMargin = data;
		}	
	}
}